package com.pvo.cfg;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class TypeMatchesConfig {
	
	private static final String dbtype_oracle = "oracle";
	private static final String dbtype_mysql = "mysql";
	private static final List<String> dbtypeList = new ArrayList<String>();
	private static final String ELE_dbtype = "dbtype";
	private static final String ELE_matchs = "matchs";
	private static final String ELE_colMatch = "colMatch";
	private static final String ELE_colType = "colType";
	private static final String ELE_valueType = "valueType";
	private static final String ELE_filedType = "filedType";
	private static List<TypeMatch> typeMatchList = null;
	static {
		dbtypeList.add(dbtype_oracle);
		dbtypeList.add(dbtype_mysql);
		typeMatchList = parseTypeMatchs();

	}
	
	public static TypeMatch getTypeMatch(String dbtype) {
		if (StringUtils.isEmpty(dbtype)) {
			return null;
		}
		boolean isDB = false;
		for (String db : dbtypeList) {
			if (db.equalsIgnoreCase(dbtype)) {
				isDB = true;
				break;
			}
		}
		if (!isDB) {
			throw new RuntimeException("not support dbtype:" + dbtype);
		}

		for (TypeMatch typeMatch : typeMatchList) {
			if (typeMatch.getDbtype().equalsIgnoreCase(dbtype)) {
				return typeMatch;
			}
		}
		return null;
	}



	@SuppressWarnings("unchecked")
	private static List<TypeMatch>  parseTypeMatchs() {
		SAXBuilder builder = new SAXBuilder();
		Document doc = null;
		try {
			doc = builder.build(new File("src/main/resources/typematchs.xml"));
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Element typematchsEle = doc.getRootElement();
		List<Element> childEles = typematchsEle.getChildren();
		List<TypeMatch> typeMatchList = new ArrayList<TypeMatch>();
		if (childEles != null && childEles.size() > 0) {
			for (Element typeMatchEle : childEles) {
				TypeMatch typeMatch = new TypeMatch();
				String dbtype = typeMatchEle.getChildTextTrim(ELE_dbtype);
				if (StringUtils.isEmpty(dbtype)) {
					System.err.println("请在typematchs.xml配置typeMatch的dbtype类型。");
					continue;
				}
				typeMatch.setDbtype(dbtype);
				Element matchesEle = typeMatchEle.getChild(ELE_matchs);
				if (matchesEle == null) {
					System.err.println("请在typematchs.xml配置typeMatch的matches类型。");
					continue;
				}
				List<ColMatch> colMatchs = new ArrayList<ColMatch>();
				List<Element> colMatchEleList = matchesEle.getChildren(ELE_colMatch);
				if (colMatchEleList != null && colMatchEleList.size() > 0) {
					for (Element colMatchEle : colMatchEleList) {
						String colType = colMatchEle.getChildTextTrim(ELE_colType);
						String valueType = colMatchEle.getChildTextTrim(ELE_valueType);
						String filedType = colMatchEle.getChildTextTrim(ELE_filedType);
						ColMatch colMatch = new ColMatch(valueType, colType, filedType);
						colMatchs.add(colMatch);
					}
					typeMatch.setMatchs(colMatchs);
				}
				typeMatchList.add(typeMatch);
			}
		}
		return typeMatchList;
	}
	
	public static void main(String[] args) {
		System.out.println(typeMatchList);
	}
}
